package weaver.jobs.cetc.duss.cron;

import weaver.common.DateUtil;
import weaver.conn.RecordSetDataSource;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.jobs.cetc.duss.utils.CetcUtil;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import static weaver.common.DateUtil.getDateTime;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-09-02
 * @Description: E8：根据浪潮查询视图获取数据，判断多个日期减去30天，则调用流程创建接口
 * @ModifyLog:
 **/

public class WeaHggfzztxCronJob extends BaseCronJob {
    @Override
    public void execute() {

        BaseBean baseBean = new BaseBean();
        CetcUtil cetcUtil = new CetcUtil();
        String formid = "580";
        RecordSetDataSource rds = new RecordSetDataSource("lc");
        rds.execute("select LSWLDW_DWMC,LSWLDW_CUSTOMFIELD69,LSWLDW_CUSTOMFIELD70,LSWLDW_CUSTOMFIELD71," +
                "LSWLDW_CUSTOMFIELD72,LSWLDW_CUSTOMFIELD73,LSWLDW_CUSTOMFIELD74,LSWLDW_CUSTOMFIELD75" +
                ",LSWLDW_CUSTOMFIELD76,LSWLDW_CUSTOMFIELD77,LSWLDW_CUSTOMFIELD78,LSWLDW_CUSTOMFIELD79," +
                "LSWLDW_CUSTOMFIELD80,LSWLDW_CUSTOMFIELD89,LSWLDW_CUSTOMFIELD65 from VW_CETE58_GYS");
        while (rds.next()) {
            //供应商名称
            String LSWLDW_DWMC = Util.null2String(rds.getString("LSWLDW_DWMC"));
            //采购组
            String LSWLDW_CUSTOMFIELD89 = Util.null2String(rds.getString("LSWLDW_CUSTOMFIELD89"));
            //合格供方编号
            String LSWLDW_CUSTOMFIELD65 = Util.null2String(rds.getString("LSWLDW_CUSTOMFIELD65"));
            String userid = cetcUtil.getUserIdByName(LSWLDW_CUSTOMFIELD89);
            baseBean.writeLog(">>>>WeaHggfzztxCronJob>>>>>userid=" + userid);
            //ISO9001
            String LSWLDW_CUSTOMFIELD69 = Util.null2String(rds.getString("LSWLDW_CUSTOMFIELD69"));
            if (!"".equals(LSWLDW_CUSTOMFIELD69)) {
                String type = "ISO9001";
                createRequest(type, LSWLDW_CUSTOMFIELD69, userid, formid, LSWLDW_CUSTOMFIELD89, LSWLDW_DWMC, LSWLDW_CUSTOMFIELD65);
            }
            //IFA16949
            String LSWLDW_CUSTOMFIELD70 = Util.null2String(rds.getString("LSWLDW_CUSTOMFIELD70"));
            if (!"".equals(LSWLDW_CUSTOMFIELD70)) {
                String type = "IFA16949";
                createRequest(type, LSWLDW_CUSTOMFIELD70, userid, formid, LSWLDW_CUSTOMFIELD89, LSWLDW_DWMC, LSWLDW_CUSTOMFIELD65);
            }

            //武器装备质量体系证书
            String LSWLDW_CUSTOMFIELD71 = Util.null2String(rds.getString("LSWLDW_CUSTOMFIELD71"));
            if (!"".equals(LSWLDW_CUSTOMFIELD71)) {
                String type = "武器装备质量体系证书";
                createRequest(type, LSWLDW_CUSTOMFIELD71, userid, formid, LSWLDW_CUSTOMFIELD89, LSWLDW_DWMC, LSWLDW_CUSTOMFIELD65);
            }

            //CY资格证书
            String LSWLDW_CUSTOMFIELD72 = Util.null2String(rds.getString("LSWLDW_CUSTOMFIELD72"));
            if (!"".equals(LSWLDW_CUSTOMFIELD72)) {
                String type = "CY资格证书";
                createRequest(type, LSWLDW_CUSTOMFIELD72, userid, formid, LSWLDW_CUSTOMFIELD89, LSWLDW_DWMC, LSWLDW_CUSTOMFIELD65);
            }

            //BM资格证书
            String LSWLDW_CUSTOMFIELD73 = Util.null2String(rds.getString("LSWLDW_CUSTOMFIELD73"));
            if (!"".equals(LSWLDW_CUSTOMFIELD73)) {
                String type = "BM资格证书";
                createRequest(type, LSWLDW_CUSTOMFIELD73, userid, formid, LSWLDW_CUSTOMFIELD89, LSWLDW_DWMC, LSWLDW_CUSTOMFIELD65);
            }

            //职业健康安全证书
            String LSWLDW_CUSTOMFIELD74 = Util.null2String(rds.getString("LSWLDW_CUSTOMFIELD74"));
            if (!"".equals(LSWLDW_CUSTOMFIELD74)) {
                String type = "职业健康安全证书";
                createRequest(type, LSWLDW_CUSTOMFIELD74, userid, formid, LSWLDW_CUSTOMFIELD89, LSWLDW_DWMC, LSWLDW_CUSTOMFIELD65);
            }

            //环境管理体系证书
            String LSWLDW_CUSTOMFIELD75 = Util.null2String(rds.getString("LSWLDW_CUSTOMFIELD75"));
            if (!"".equals(LSWLDW_CUSTOMFIELD75)) {
                String type = "环境管理体系证书";
                createRequest(type, LSWLDW_CUSTOMFIELD75, userid, formid, LSWLDW_CUSTOMFIELD89, LSWLDW_DWMC, LSWLDW_CUSTOMFIELD65);
            }

            //授权代理证书
            String LSWLDW_CUSTOMFIELD76 = Util.null2String(rds.getString("LSWLDW_CUSTOMFIELD76"));
            if (!"".equals(LSWLDW_CUSTOMFIELD76)) {
                String type = "授权代理证书";
                createRequest(type, LSWLDW_CUSTOMFIELD76, userid, formid, LSWLDW_CUSTOMFIELD89, LSWLDW_DWMC, LSWLDW_CUSTOMFIELD65);
            }

            //安全许可证书
            String LSWLDW_CUSTOMFIELD77 = Util.null2String(rds.getString("LSWLDW_CUSTOMFIELD77"));
            if (!"".equals(LSWLDW_CUSTOMFIELD77)) {
                String type = "安全许可证书";
                createRequest(type, LSWLDW_CUSTOMFIELD77, userid, formid, LSWLDW_CUSTOMFIELD89, LSWLDW_DWMC, LSWLDW_CUSTOMFIELD65);
            }

            //危化品证书
            String LSWLDW_CUSTOMFIELD78 = Util.null2String(rds.getString("LSWLDW_CUSTOMFIELD78"));
            if (!"".equals(LSWLDW_CUSTOMFIELD78)) {
                String type = "危化品证书";
                createRequest(type, LSWLDW_CUSTOMFIELD77, userid, formid, LSWLDW_CUSTOMFIELD89, LSWLDW_DWMC, LSWLDW_CUSTOMFIELD65);
            }

            //非药品备案
            String LSWLDW_CUSTOMFIELD79 = Util.null2String(rds.getString("LSWLDW_CUSTOMFIELD79"));
            if (!"".equals(LSWLDW_CUSTOMFIELD79)) {
                String type = "非药品备案";
                createRequest(type, LSWLDW_CUSTOMFIELD77, userid, formid, LSWLDW_CUSTOMFIELD89, LSWLDW_DWMC, LSWLDW_CUSTOMFIELD65);
            }

            //道路运输许可证书
            String LSWLDW_CUSTOMFIELD80 = Util.null2String(rds.getString("LSWLDW_CUSTOMFIELD80"));
            if (!"".equals(LSWLDW_CUSTOMFIELD80)) {
                String type = "道路运输许可证书";
                createRequest(type, LSWLDW_CUSTOMFIELD77, userid, formid, LSWLDW_CUSTOMFIELD89, LSWLDW_DWMC, LSWLDW_CUSTOMFIELD65);
            }

        }

    }

    public void createRequest(String type, String rq_fieldid, String userid, String formid, String cgz, String gfmc, String hggfbh) {
        BaseBean baseBean = new BaseBean();
        CetcUtil cetcUtil = new CetcUtil();
        String currentDate = DateUtil.getCurrentDate();
        String formatDate = cetcUtil.formatDateToOA(rq_fieldid);
        Calendar cal = DateUtil.addDay(DateUtil.getCalendar(formatDate), -30);
        String rq = DateUtil.getCalendarDate(cal);
        baseBean.writeLog(">>>>WeaHggfzztxCronJob>>>>>rq" + rq + ",currentDate=" + currentDate);
        if (currentDate.equals(rq) && !userid.equals("")) {
            Map<String, String> wf = cetcUtil.getWorkflowId(formid);
            String workflowid = Util.null2String(wf.get("workflowid"));
            String requestname = Util.null2String(wf.get("workflowname")) + "-" + cgz + "-" + type + "-" + getDateTime();
            Map<String, String> requestMainMap = new HashMap<>();
            requestMainMap.put("sqr", userid);
            requestMainMap.put("gfmc", gfmc);
            requestMainMap.put("zzmc", type);
            requestMainMap.put("zzdqrq", formatDate);
            requestMainMap.put("hggfbh", hggfbh);
            String newRequestid = cetcUtil.WorkflowCreateByRequestMap(userid, workflowid, requestname, "0", requestMainMap);
            baseBean.writeLog(">>>>WeaHggftxCronJob>>>>>newRequestid=" + newRequestid);
        }
    }

}
